//
//  ACDemoRootViewController.m
//  ACFramework_Example
//
//  Created by caoyq on 2018/12/15.
//  Copyright © 2018 honeycao. All rights reserved.
//

#import "ACDemoRootViewController.h"

@interface ACDemoRootViewController ()
{
    NSArray *_list;
}

@end

@implementation ACDemoRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self readListFromJson];
}

- (void)readListFromJson
{
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:NSStringFromClass([self class]) ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    _list = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    NSDictionary *dic = _list[indexPath.row];
    cell.textLabel.text = dic[@"title"];
    cell.detailTextLabel.text = dic[@"desc"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = _list[indexPath.row];
    NSString *vcName = dic[@"vcName"];
    Class cls = NSClassFromString(vcName);
    UIViewController *vc = [[cls alloc] init];
    vc.title = dic[@"title"];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
